/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package its.serviceSystem.Bean;

import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import mx.its.dds2.ServicesSystem.Modelo.Service;
import mx.its.dds2.serviceSystem.Bl.ServiceBlLocal;

/**
 *
 * @author Carlos Asis
 */
@ManagedBean
@SessionScoped
public class ServiceBean {

   @EJB
    private ServiceBlLocal serviceBl;

    
    private Service service;
    private List<Service>  listServices;
    private int b=0;
    
    public ServiceBean() {
        service=new Service();
    }

    public Service getService() {
        return service;
    }

    public void setService(Service service) {
        this.service = service;
    }

    public List<Service> getServices() {
        listServices=serviceBl.getListAll();
        return listServices;
    }

    public void setServices(List<Service> listServices) {
        this.listServices = listServices;
    }
    
  
    
    public String prepararNuevo(){
        b=1;
        service = new Service();
        return "ServiceCrearEditar";
    }
    
    public void prepararEditar(ActionEvent a){
        b=2;
        String cId=a.getComponent().getClientId().split(":")[2];
        int id= Integer.parseInt(cId);
        service=listServices.get(id);
    }
    
    public String guardar(){
        switch (b) {
            case 1:
               serviceBl.register(service);
                break;
            case 2:
               serviceBl.modify(service);
                break;
            case 3:
               //serviceBl.r(service);
        }
        return "ServiceFacelets";
    }
    
    public void eliminar(ActionEvent a){
        b=3;
        String cId = a.getComponent().getClientId().split(":")[2];
        int id = Integer.parseInt(cId);
        service = listServices.get(id);
        guardar();
    }
}
